using System;
using System.Collections.Generic;
using System.Linq;

namespace TestConsole3
{
    /// <summary>
    /// 无序排列组合
    /// </summary>
    public class UnOrder
    {
        //结果
        List<List<int>> result = new List<List<int>>();

        //自然数
        List<int> data = new List<int>();

        //临时存放数据
        List<int> temp = new List<int>();

        /// <summary>
        /// 获取排列组合
        /// </summary>
        /// <param name="listCount">集合长度</param>
        /// <param name="getLen">单个组合长度</param>
        /// <returns></returns>
        public List<List<int>> GetCombinData(int listCount, int getLen)
        {
            for (int i = 1; i <= listCount; i++)
            {
                data.Add(i);
                temp.Add(0);
            }
            GetLen(getLen);
            return result;
        }

        void Test()
        {
            for (int i = 0; i < data.Count; i++)
            {
                temp.Add(0);
            }
            // for (int i = 2; i <= data.Count; i++)
            // {
            GetLen(4);
            //}
            var distResult = result.Distinct();
            foreach (var item in distResult)
            {
                foreach (var obj in item)
                {
                    Console.Write(obj + ",");
                }
                Console.WriteLine();
            }
            Console.WriteLine($"totle:{distResult.Count()}");
        }

        /// <summary>
        /// 递归每次都是2x2矩阵
        /// </summary>
        /// <param name="constNum"></param>
        void GetNum(int constNum)
        {
            for (int F = 0; F < data.Count; F++)
            {
                if ((constNum - 1) == F) continue;
                //Console.WriteLine($"{data[constNum - 1]},{data[F]}");
                temp[0] = data[F];
                temp[1] = data[constNum - 1];
                var currentCombin = temp.FindAll(x => x > 0);
                currentCombin.Reverse();
                if (currentCombin.Count() == currentCombin.Distinct().Count()) result.Add(currentCombin);
            }
            if (constNum < data.Count) GetNum(constNum + 1);
        }

        /// <summary>
        /// 获取固定长度的组合
        /// </summary>
        /// <param name="len"></param>
        void GetLen(int len)
        {
            if (len == 2) { GetNum(1); }
            else
            {
                for (int i = 1; i <= data.Count; i++)
                {
                    //Console.WriteLine($"start:{i}");
                    temp[len - 1] = i;
                    if (len > 2) GetLen(len - 1);
                }
            }
        }

    }
}